package com.dlwx.parttimetalent.ui;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.dlwx.parttimetalent.R;
import com.dlwx.parttimetalent.base.BaseActivity;
import com.dlwx.parttimetalent.bean.BaseBean;
import com.dlwx.parttimetalent.comm.Constant;
import com.dlwx.parttimetalent.comm.Http;
import com.dlwx.parttimetalent.utils.DealResult;
import com.dlwx.parttimetalent.utils.OkgoUtil;
import com.dlwx.parttimetalent.utils.SPUtils;
import com.dlwx.parttimetalent.utils.TimeCount;
import com.dlwx.parttimetalent.utils.ToastUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SetPayPswActivity extends BaseActivity {

    @BindView(R.id.title_left_one_btn)
    ImageView titleLeftOneBtn;
    @BindView(R.id.tv_middle_title)
    TextView tvMiddleTitle;
    @BindView(R.id.tv_get_yzm)
    TextView tvGetYzm;
    @BindView(R.id.tv_confirm)
    TextView tvConfirm;
    @BindView(R.id.et_yzm)
    EditText etYzm;
    @BindView(R.id.tv_remark)
    TextView tvRemark;
    private TimeCount timeCount = null;
    private String phone;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_pay_psw);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
         phone = (String) SPUtils.get(this, Constant.PHONE, "");
        titleLeftOneBtn.setVisibility(View.VISIBLE);
        tvMiddleTitle.setVisibility(View.VISIBLE);
        tvMiddleTitle.setText("设置支付密码");
        tvRemark.setText("请输入您的手机号"+phone+"收到的短信验证码");
    }


    @OnClick({R.id.title_left_one_btn, R.id.tv_get_yzm, R.id.tv_confirm})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_left_one_btn:
                this.finish();
                break;
            case R.id.tv_get_yzm:
                getYzm();
                break;
            case R.id.tv_confirm:
                confirm();
                break;
        }
    }

    private void getYzm() {
        OkgoUtil.postNoUser(Http.YZM, this)
                .params("phone_no", phone)
                .params("status", 5)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        if (response == null) {
                            return;
                        }
                        String res = response.body().toString();
                        BaseBean bean = new Gson().fromJson(res, BaseBean.class);
                        if (bean == null) {
                            return;
                        };
                        if (bean.getCode() == Constant.SUCESS) {
                            timeCount = new TimeCount(tvGetYzm, 2 * 60 * 1000, 1000,"#FF4081");
                            timeCount.start();
                        }
                        ToastUtils.toastForShort(SetPayPswActivity.this, bean.getResult());
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        ToastUtils.toastForShort(SetPayPswActivity.this, getString(R.string.bad_net));
                    }
                });

    }

    private void confirm() {
        String yzm = etYzm.getText().toString();
        if (TextUtils.isEmpty(yzm)) {
            ToastUtils.toastForShort(this, "验证码不能为空");
            return;
        }
        String phone = (String) SPUtils.get(this, Constant.PHONE, "");
        OkgoUtil.post(Http.CheckCode, SetPayPswActivity.this)
                .params("phone_no", phone)
                .params("codeno", yzm)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        BaseBean parentBean = DealResult.getInstance().dealDatas(SetPayPswActivity.this
                                , response, new TypeToken<BaseBean>() {
                                }.getType());
                        if (parentBean == null) {
                            return;
                        }
                        int code = parentBean.getCode();
                        if (code == Constant.SUCESS) {
                            SetPayPswActivity.this.finish();
                            Intent i=new Intent(SetPayPswActivity.this,SetPswActivity.class);
                            i.putExtra("flag",1);
                            startActivity(i);
                        }else{

                            ToastUtils.toastForShort(SetPayPswActivity.this,parentBean.getResult());
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        ToastUtils.toastForShort(SetPayPswActivity.this, getString(R.string.bad_net));
                    }
                });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (timeCount != null) {
            timeCount.cancel();
            timeCount = null;
        }
    }
}
